﻿using ExDUIR.NET.Declaration;
using ExDUIR.NET.Handle;
using ExDUIR.NET.Canvas;
using System;

namespace ExDuiR.NET.Frameworks.Graphics
{
    class ExTheme
    {
        private readonly ExThemeHandle _handle;
        public bool IsValid => _handle.IsInvalid;
        public IntPtr Handle => _handle.Handle;
        internal ExTheme(ExThemeHandle handle)
        {
            _handle = handle;
        }
        protected int _color = 0;

        internal ExTheme(string lptszFile, byte[] lpKey, IntPtr dwKeyLen, bool bDefault)
        {
            _handle = new ExThemeHandle(ExAPI.Ex_ThemeLoadFromFile(lptszFile, lpKey, dwKeyLen, bDefault));
        }

        internal ExTheme(byte[] lpData, IntPtr dwDataLen, byte[] lpKey, IntPtr dwKeyLen, bool bDefault)
        {
            _handle = new ExThemeHandle(ExAPI.Ex_ThemeLoadFromMemory(lpData, dwDataLen, lpKey, dwKeyLen, bDefault));
        }

        public bool DrawControl(ExCanvas canvas, float dstLeft, float dstTop, float dstRight, float dstBottom, int atomClass, int atomSrcRect, int dwAlpha)
        {
            return ExAPI.Ex_ThemeDrawControl(_handle.Handle, canvas.Handle, dstLeft, dstTop, dstRight, dstBottom, atomClass, atomSrcRect, dwAlpha);
        }

        public bool DrawControlEx(ExCanvas canvas, float dstLeft, float dstTop, float dstRight, float dstBottom, int atomClass, int atomSrcRect, int atomBackgroundRepeat, int atomBackgroundPositon, int atomBackgroundGrid, int atomBackgroundFlags, int dwAlpha)
        {
            return ExAPI.Ex_ThemeDrawControlEx(_handle.Handle, canvas.Handle, dstLeft, dstTop, dstRight, dstBottom, atomClass, atomSrcRect, atomBackgroundRepeat, atomBackgroundPositon, atomBackgroundGrid, atomBackgroundFlags, dwAlpha);
        }

        public int GetColor(int nIndex)
        {
            return ExAPI.Ex_ThemeGetColor(_handle.Handle, nIndex);
        }

        public IntPtr GetValuePtr(int atomClass, int atomProp)
        {
            IntPtr ptr = ExAPI.Ex_ThemeGetValuePtr(_handle.Handle, atomClass, atomProp);
            return ptr;
        }

        public bool Dispose() => _handle.Release();
    }
}
